﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineTaskCommand.Commands;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineTaskCommand.CommandHandlers
{
    public class AllDeleteTaskCommandHandler : IRequestHandler<AllDeleteTaskCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<ExamineTask> _examinetaskRepository;
        public AllDeleteTaskCommandHandler(IBaseRepository<ExamineTask> examinetaskRepository)
        {
            _examinetaskRepository = examinetaskRepository;
        }
        public async Task<APIRequst<int>> Handle(AllDeleteTaskCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();

            var idlist = request.taskcode.Split(',').Select(x => Convert.ToInt32(x)).ToList();

            if (idlist == null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
                return await Task.FromResult(result);
            }
            var listment = _examinetaskRepository.GetAll().Where(x => idlist.Contains(x.TaskId)).ToList();
            foreach (var item in listment)
            {
                item.isDel = true;
            }
            var code = await _examinetaskRepository.UpdateRange(listment);
            if (code > 0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();

            }
            return await Task.FromResult(result);

        }
    }
}
